package com.bawei.a2010mdm

import android.content.Context
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.a2010mdm.delegate.MyAppRequestListener
import com.bawei.a2010mdm.delegate.MyComponentDelegate
import com.bawei.a2010mdm.delegate.MyPhoneInfoDelegate
import com.bawei.a2010mdm.delegate.MyTaskDescriptionDelegate
import com.bawei.library_base.BaseApplication
import com.bawei.library_base.utils.GlobalThreadPools
import com.blankj.utilcode.util.SPUtils
import com.lody.virtual.client.core.VirtualCore
import com.lody.virtual.client.stub.VASettings
import com.tencent.bugly.crashreport.CrashReport
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import leakcanary.LeakCanary


class App: BaseApplication() {
    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        VASettings.ENABLE_IO_REDIRECT = true
        VASettings.ENABLE_INNER_SHORTCUT = false
        VirtualCore.get().startup(this) //开启虚拟机
    }



    override fun onCreate() {
        super.onCreate()
        //TODO 1:预初始化
        //设置LOG开关，默认为false
        UMConfigure.setLogEnabled(true)
        //友盟预初始化
        UMConfigure.preInit(applicationContext, "5f98c674e91fe51466b20376", "Umeng")
//        // 自动采集选择
//        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)
        //手动采集选择
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.MANUAL);
        //TODO 2：第一次打开弹出对话框提示是否同意
        SPUtils.getInstance().put("uminit",true) //默认是同意
        //TODO 3：
        /**
         * 打开app首次隐私协议授权，以及sdk初始化，判断逻辑请查看SplashTestActivity
         */
        //判断是否同意隐私协议，uminit为1时为已经同意，直接初始化umsdk
        if (SPUtils.getInstance().getBoolean("uminit")) {
            //友盟正式初始化
            UMConfigure.init(this,UMConfigure.DEVICE_TYPE_PHONE,null)
        }





        //启动优化：放在线程池中
        GlobalThreadPools.getInstance().execute {
            //ARouter初始化
            ARouter.openDebug();
            ARouter.openLog();
            ARouter.init(this);//注意！！！init放在后面

            LeakCanary
        }

        //bugly初始化：官网建议不要放
        CrashReport.initCrashReport(getApplicationContext(), "93323b9f06", true);

        //启动优化：放在线程池中
        GlobalThreadPools.getInstance().execute{
            //初始化虚拟机
            val virtualCore = VirtualCore.get()
            virtualCore.initialize(object : VirtualCore.VirtualInitializer() {
                override fun onMainProcess() {
//                Once.initialise(this@App)
//                FlurryAgent.Builder()
//                    .withLogEnabled(true)
//                    .withListener {}
//                    .build(this@App, "48RJJP7ZCZZBB6KMMWW5")
                }

                override fun onVirtualProcess() {
                    //listener components
                    virtualCore.componentDelegate = MyComponentDelegate()
                    //fake phone imei,macAddress,BluetoothAddress
                    virtualCore.phoneInfoDelegate = MyPhoneInfoDelegate()
                    //fake task description's icon and title
                    virtualCore.taskDescriptionDelegate = MyTaskDescriptionDelegate()
                }

                override fun onServerProcess() {
                    virtualCore.setAppRequestListener(MyAppRequestListener(this@App))
                    virtualCore.addVisibleOutsidePackage("com.tencent.mobileqq")
                    virtualCore.addVisibleOutsidePackage("com.tencent.mobileqqi")
                    virtualCore.addVisibleOutsidePackage("com.tencent.minihd.qq")
                    virtualCore.addVisibleOutsidePackage("com.tencent.qqlite")
                    virtualCore.addVisibleOutsidePackage("com.facebook.katana")
                    virtualCore.addVisibleOutsidePackage("com.whatsapp")
                    virtualCore.addVisibleOutsidePackage("com.tencent.mm")
                    virtualCore.addVisibleOutsidePackage("com.immomo.momo")
                }
            })
        }


    }


}